<?php
$today = date('Y-m-d');
$_today = strtotime($today) . "000";

$match_timestamp = Frontend::findMatchByTimeStamp((float) $_today)->count();
//var_dump($match_timestamp);die;
if ($page == NULL) {
    $_page = 1;
} else {
    $_page = $page;
}

$numPage = ceil($match_timestamp / $num_page);

$pageCurrent = $num_page * ($_page - 1);
$match_all = Frontend::findMatchByTimeStamp((float) $_today)->skip($pageCurrent)->limit($num_page);
$arr_match = array();
$arr = iterator_to_array($match_all);

foreach ($match_all as $keys => $_match_all) {
    $datematch = explode(" ", $_match_all["DateMatch"]);
    $arr_match[$datematch[0]][] = $arr[$keys];
}
//echo "<pre>";
//var_export(iterator_to_array($match_all)); 
?>
<div id="main" class="row">
    <div class="span3">
        <ul class="nav nav-list nav-list-vivid">
            <li class="nav-header">Giải đấu</li>
            <li><a href="#soccer">Tất cả</a></li>
            <?php
            $country = Frontend::findAllCountry("Anh");
            foreach ($country as $_country) {
                ?>
                <li class="nav-header"><?php echo $_country["name"] ?></li>
                <?php
                $tournament = Frontend::findTournamentByCountry($_country["_id"]);
                foreach ($tournament as $_tournament) {
                    ?>
                    <li class="children">
                        <a href="#soccer/england/premier-league">
                            <img src="skin/frontend/caulacbo/oYVHSYPZZ4.png" class="logo" width="20">
                            <?php echo $_tournament["title"]; ?>                
                            <span class="label label-inverse pull-right">94</span>
                        </a>
                    </li>
                <?php } ?>
            <?php } ?>
        </ul>
    </div>

    <div class="span9">
        <div class="headnav clearfix" style="margin-bottom: 18px;">
            <!--            <div class="btn-group">
                            <a class="btn" href="#soccer/all/all/results">Kết quả</a>
                            <a class="btn active">Sắp diễn ra</a>
                            <a href="#" class="active btn btn-warning dropdown-toggle" data-toggle="dropdown">
                                <span class="caret"></span>
                            </a>
                            <ul class="dropdown-menu dropdown-special" style="margin-left: 30px">
                                <li><a href="#soccer">Sắp diễn ra</a></li>
                                <li><a href="#soccer/all/all/2013-09-22">Hôm nay, 22/09</a></li>
                                <li><a href="#soccer/all/all/2013-09-23">Monday, 23/09</a></li>
                                <li><a href="#soccer/all/all/2013-09-24">Tuesday, 24/09</a></li>
                                <li><a href="#soccer/all/all/2013-09-25">Wednesday, 25/09</a></li>
                                <li><a href="#soccer/all/all/2013-09-26">Thursday, 26/09</a></li>
                                <li><a href="#soccer/all/all/2013-09-27">Friday, 27/09</a></li>
                                <li><a href="#soccer/all/all/2013-09-28">Saturday, 28/09</a></li>
                                <li><a href="#soccer/all/all/2013-09-29">Sunday, 29/09</a></li>
                            </ul>
                        </div>-->
            <div class="btn-group pull-right">
                <a class="btn" href="#"><?php echo $match_timestamp ?> kết quả</a>
                <?php
                if(($page - 1) > 4){
                    $j = $page - 3;
                }else{
                    $j = 1;
                }
                if(($numPage - $page) > 3){
                    $k = $page + 2;
                }else{
                    $k = $numPage;
                }
                if ($page > 1) {
                    ?>
                    <a class="btn" href="<?php echo SKIN_FPATH . "dudoan/" . ($_page - 1) ?>">Prev</a>
                <?php } ?>
                <?php
                for ($i = $j; $i <= $k; $i++) {
                    if ($i == 8 && $page < $numPage) {
                        ?>
                        <a class="btn disabled" href="#">...</a>
                        <a class="btn" href="<?php echo SKIN_FPATH . "dudoan/" . $numPage ?>"><?php echo $numPage; ?></a>
                        <?php
                        break;
                    } elseif ($i == 2 && $page == $numPage) {
                        ?>
                        <a class="btn disabled" href="#">...</a>   
                        <?php
                    } else {
                        ?>
                        <a class="btn <?php
                        if ($i == $page) {
                            echo "active";
                        }
                        ?>" href="<?php echo SKIN_FPATH . "dudoan/" . $i ?>"><?php echo $i; ?></a>
                           <?php
                       }
                   }
                   ?>
                   <?php if ($page < $numPage) { ?>
                    <a class="btn" href="<?php echo SKIN_FPATH . "dudoan/" . ($_page + 1) ?>">Next</a>
                <?php } ?>
            </div>
        </div>


        <table class="table table-bordered table-events">
            <?php
            foreach ($arr_match as $key => $_arr_match) :
                $date = date('l, Y-m-d', strtotime($key));
//            echo "<pre>";
//            var_export($arr_match); 
                //Lấy ngày hiện tại
//                if ($i == 0) {
//                    $today = date('Y-m-d');
//                    $day = strtotime(date("Y-m-d", strtotime($today)) . " +1 day");
//                    $day = strftime("%Y-%m-%d", $day);
//                    $match_date = Frontend::findMatchByDate($today);
//                    $_today = date('l, Y-m-d');
//                } else {
//                    //Cộng thêm 1 ngày
//                    $day = strtotime(date("Y-m-d", strtotime($day)) . " +1 day");
//                    $day = strftime("%Y-%m-%d", $day);
//                    $match_date = Frontend::findMatchByDate($day);
//                    $_day = date('l, Y-m-d', strtotime($day));
//                }
                ?>
                <thead>
                    <tr>
                        <th colspan="10">
                            <i class="icon-calendar"></i>
                            <?php echo $date; ?>
                        </th>
                    </tr>
                </thead>

                <tbody>
                    <tr class="head">
                        <td rowspan="2" class="time">Giờ</td>
                        <td class="p" rowspan="2" colspan="2">Trận</td>
                        <td colspan="3">1X2 Toàn Trận</td>
                        <td rowspan="2">Chấp Toàn Trận</td>
                        <td rowspan="2">Trên / Dưới</td>
                        <td rowspan="2"></td>
                    </tr>
                    <tr class="head">
                        <td>1</td>
                        <td>X</td>
                        <td>2</td>
                    </tr>
                    <?php
                    // Tìm Match theo ngày
                    foreach ($_arr_match as $_match_date) :
                        $team1 = Frontend::findTeamById((int) $_match_date["Team1"]);
                        $team2 = Frontend::findTeamById((int) $_match_date["Team2"]);
                        $odd = Frontend::findOddByMacth((int) $_match_date["_id"]);
                        //if (iterator_to_array($odd)):
                        ?>
                        <tr class="odd">
                            <td rowspan="2" class="time">
                                <i class="icon-time"></i><br>
                                <?php echo $_match_date["StartTime"] ?>
                            </td>
                            <td class="p" colspan="2">
                                <?php
                                foreach ($team1 as $_team1) :
                                    echo $_team1["name"] . " - " . $_team1["_id"];
                                endforeach;
                                ?>  
                            </td>
                            <?php
                            if (iterator_to_array($odd)) {
                                foreach ($odd as $_odd):
                                    ?>
                                    <!--------------- 1X2 Toàn Trận  --------------------------->
                                    <?php
                                    if ($_odd["typeID"] == "ODD_FT") :
                                        $value = explode("|", $_odd["value"]);

                                        if ($value[0] != "" && $value[0] != "chưa có tỷ lệ") {
                                            foreach ($value as $key => $_value):
                                                $data = array('id' => $_odd['_id'], 'value' => $_value);
                                                $encode = json_encode($data);
                                                $base_encode = base64_encode($encode);
                                                ?>
                                                <?php if ($key == 0) { ?>
                                                    <td rowspan="2" class="odds">    
                                                        <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                    </td>
                                                <?php } elseif ($key == 1) { ?>
                                                    <td rowspan="2" class="odds decrease">    
                                                        <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                    </td>
                                                <?php } elseif ($key == 2) { ?>
                                                    <td rowspan="2" class="odds increase">    
                                                        <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                    </td>
                                                <?php } ?>
                                            <?php endforeach; ?>
                                        <?php } else { ?>
                                            <td rowspan="2" colspan="3" class="odds increase">    
                                                Chưa có tỷ lệ.
                                            </td>    
                                        <?php } ?>
                                    <?php endif; ?>
                                    <!--------------- Kết Thúc 1X2 Toàn Trận  --------------------------->
                                    <!--------------- Chấp Toàn Trận  --------------------------->
                                    <?php if ($_odd["typeID"] == "ODD_FTHDP") : ?>

                                        <td style="text-align: right;" class="odds odds-large">
                                            <?php
                                            $value = explode("|", $_odd["value"]);
                                            foreach ($value as $key => $_value):
                                                $data = array('id' => $_odd['_id'], 'value' => $_value);
                                                $encode = json_encode($data);
                                                $base_encode = base64_encode($encode);
                                                ?>
                                                <?php if ($key == 0) { ?>
                                                    <span class="pull-left label label-warning"><?php echo $_value ?></span> 
                                                <?php } elseif ($key == 1) { ?>
                                                    <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                                <?php } ?>
                                            <?php endforeach; ?>
                                        </td>
                                    <?php endif; ?>
                                    <!--------------- Kết Thúc Chấp Toàn Trận  --------------------------->
                                    <!--------------- Trên / Dưới  --------------------------->
                                    <?php if ($_odd["typeID"] == "ODD_UO") : ?>
                                        <td style="text-align: right;" class="odds odds-large">    
                                            <?php
                                            $value = explode("|", $_odd["value"]);
                                            foreach ($value as $key => $_value):
                                                $data = array('id' => $_odd['_id'], 'value' => $_value);
                                                $encode = json_encode($data);
                                                $base_encode = base64_encode($encode);
                                                ?>
                                                <?php if ($key == 0) { ?>
                                                    <span class="pull-left label label-warning"><?php echo $value[0] ?></span> 
                                                <?php } elseif ($key == 1) { ?>
                                                    <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $value[1] ?></a>
                                                <?php } ?>
                                            <?php endforeach; ?>
                                        </td>
                                    <?php endif; ?>
                                    <!--------------- Kết Thúc Trên / Dưới  --------------------------->
                                <?php endforeach; ?>
                            <?php } else { ?>
                                <td rowspan="2" colspan="3" class="odds increase">    
                                    Chưa có tỷ lệ.
                                </td>  
                                <td rowspan="2" style="text-align: left;" class="odds odds-large"> 
                                    Chưa có tỷ lệ.
                                </td>
                                <td rowspan="2" style="text-align: left;" class="odds odds-large"> 
                                    Chưa có tỷ lệ.
                                </td>
                            <?php } ?>
                            <td rowspan="2" class="go">
                                <a href="#soccer/south-korea/k-league/gyeongnam-fc-daegu-fc/2287">
                                    <img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/Flaticon_1413.png" />
                                </a>
                            </td>
                        </tr>
                        <tr class="odd">
                            <td class="p" colspan="2">
                                <?php
                                foreach ($team2 as $_team2) :
                                    echo $_team2["name"];
                                endforeach;
                                ?>  
                            </td>
                            <?php
                            foreach ($odd as $_odd):
                                ?>
                                <!--------------- Chấp Toàn Trận  --------------------------->
                                <?php if ($_odd["typeID"] == "ODD_FTHDP") : ?>
                                    <?php
                                    $value = explode("|", $_odd["value"]);
                                    foreach ($value as $key => $_value):
                                        $data = array('id' => $_odd['_id'], 'value' => $_value);
                                        $encode = json_encode($data);
                                        $base_encode = base64_encode($encode);
                                        ?>
                                        <?php if ($key == 2) { ?>
                                            <td style="text-align: right;" class="odds odds-large">    
                                                <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                            </td>
                                        <?php } ?>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                                <!--------------- Kết Thúc Chấp Toàn Trận  --------------------------->
                                <!--------------- Trên / Dưới  --------------------------->
                                <?php if ($_odd["typeID"] == "ODD_UO") : ?>
                                    <?php
                                    $value = explode("|", $_odd["value"]);
                                    foreach ($value as $key => $_value):
                                        $data = array('id' => $_odd['_id'], 'value' => $_value);
                                        $encode = json_encode($data);
                                        $base_encode = base64_encode($encode);
                                        ?>
                                        <?php if ($key == 2) { ?>
                                            <td style="text-align: right;" class="odds odds-large">    
                                                <a href="javascript:void(0)" id="<?php echo $_odd["_id"] ?>" class="add-to-betslip" data_value='<?php print_r($base_encode) ?>' onclick="addodd('<?php echo $base_encode; ?>');"><?php echo $_value ?></a>
                                            </td>
                                        <?php } ?>
                                    <?php endforeach; ?>
                                <?php endif; ?>
                                <!--------------- Kết Thúc Trên / Dưới  --------------------------->
                            <?php endforeach; ?>
                        </tr>
                        <?php //endif; ?>
                    <?php endforeach; ?>
                <?php endforeach; ?>
            </tbody>
        </table>
        <div class="clearfix" style="margin-top: 18px;">
        </div>
        <div class="btn-group pull-right">
            <a class="btn" href="#"><?php echo $match_timestamp ?> kết quả</a>
            <!--<a class="btn active" href="/soccer/all/all/coming/1">1</a>-->
            <?php for ($i = 1; $i <= $numPage; $i++) { ?>
                <a class="btn <?php
                   if ($i == $page) {
                       echo "active";
                   }
                   ?>" href="<?php echo SKIN_FPATH . "dudoan/" . $i ?>"><?php echo $i; ?></a>
               <?php } ?>
        </div>
    </div>

</div>
<script>
                            function addodd($values) {
                                $.ajax({
                                    // The link we are accessing.
                                    url: "/ajax.php",
                                    // The type of request.
                                    type: "get",
                                    data: "act=addodd&values=" + $values,
                                    // The type of data that is getting returned.
                                    dataType: "html",
                                    error: function() {
                                        // Load the content in to the page.
//                    $("#result_bets").html("<label>Page Not Found!!</label>");
                                    },
                                    success: function(strData) {
                                        // Load the content in to the page.
                                        if (strData == "true") {
                                            alert('Kèo của trận này đã được đặt');
                                        } else {
                                            $("#result_bets").html(strData);
                                        }
                                    }
                                });
                            }
</script>